package com.boot.producer.service.impl;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.boot.producer.entity.BasicInfo;
import com.boot.producer.mapper.BasicInfoMapper;
import com.boot.producer.service.BasicInfoService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.boot.producer.service.dto.WeeklyInfoDto;
import com.boot.utils.DateUtil;
import org.springframework.stereotype.Service;
import sun.reflect.Reflection;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZhangZhilei
 * @since 2019-01-29
 */
@Service
public class BasicInfoServiceImpl extends ServiceImpl<BasicInfoMapper, BasicInfo> implements BasicInfoService {

    @Override
    public WeeklyInfoDto getWeeklyInfo(List<BasicInfo> basicInfos, int days,Date end) {
        WeeklyInfoDto weeklyInfoDto = new WeeklyInfoDto();
        List<Integer> avgSpeeds = new ArrayList<>();
        List<Float> totalDistances = new ArrayList<>();
        List<Integer> totalTimes = new ArrayList<>();
        Integer dayTime = 0;
        Integer nightTime = 0;
        int j = 0;
        for (int i = days; i >= 0; i--) {
            if (j<basicInfos.size() && DateUtil.isTheSameDay(basicInfos.get(j).getTime(),DateUtil.changeDay(end,-i)) ){
                BasicInfo basicInfo = basicInfos.get(j);
                totalDistances.add(basicInfo.getTotalDistance()!=null?basicInfo.getTotalDistance():0);
                if (basicInfo.getTotalDistance()!=null && basicInfo.getTotalTime()!= null && basicInfo.getTotalDistance()!=0 && basicInfo.getTotalTime()!= 0){
                    avgSpeeds.add(Math.round(basicInfo.getTotalDistance()*60/basicInfo.getTotalTime()));
                } else {
                    avgSpeeds.add(0);
                }
                totalTimes.add(basicInfo.getTotalTime());
                nightTime += basicInfo.getNightTime()!=null?basicInfo.getNightTime():0;
                if (basicInfo.getTotalTime() != null && basicInfo.getNightTime()!=null){
                    dayTime += basicInfo.getTotalTime() - basicInfo.getNightTime();
                }
                j++;
            } else {
                System.out.println(false);
                totalDistances.add(0F);
                avgSpeeds.add(0);
                totalTimes.add(0);
            }
        }
        weeklyInfoDto.setAvgSpeeds(avgSpeeds);
        weeklyInfoDto.setDayTime(dayTime);
        weeklyInfoDto.setNightTime(nightTime);
        weeklyInfoDto.setTotalDistances(totalDistances);
        weeklyInfoDto.setTotalTimes(totalTimes);
        return weeklyInfoDto;
    }

    /**
     * @param begin 开始日期
     * @param end   结束日期
     * @return com.boot.producer.entity.BasicInfo
     * @description 根据日期范围获取基本信息
     * @author 张志磊
     * @date 2019/3/4 9:06
     */
    @Override
    public BasicInfo getInfoByDateRange(Date begin, Date end) {
        BasicInfo basicInfo = new BasicInfo();
        basicInfo.setNightTime(0);
        basicInfo.setContinueTime(0);
        basicInfo.setTotalDistance(0.0F);
        basicInfo.setTotalTime(0);
        basicInfo.setAvgSpeed(0F);
        basicInfo.setNightSpeed(0f);
        Float nightDistanceCount = 0F;
        List<BasicInfo> basicInfos = this.baseMapper.selectList(new EntityWrapper<BasicInfo>().between("time", begin, end));
        for (BasicInfo info:
                basicInfos) {
            if (null ==  info.getNightTime() || null == info.getTotalTime() || info.getTotalDistance() == null || info.getContinueTime() == null || info.getAvgSpeed() == null){
                continue;
            }
            basicInfo.setNightTime(basicInfo.getNightTime()+info.getNightTime());
            basicInfo.setTotalDistance(basicInfo.getTotalDistance()+info.getTotalDistance());
            basicInfo.setTotalTime(basicInfo.getTotalTime()+info.getTotalTime());
            if (info.getContinueTime()>basicInfo.getContinueTime()){
                basicInfo.setContinueTime(info.getContinueTime());
            }
            nightDistanceCount+= info.getNightSpeed()*info.getNightTime();
        }
        if (basicInfo.getNightTime() != 0){
            basicInfo.setNightSpeed(nightDistanceCount/basicInfo.getNightTime());
        }
        if (basicInfo.getTotalTime() !=0 ){
            basicInfo.setAvgSpeed(1.0F*60*basicInfo.getTotalDistance()/basicInfo.getTotalTime());
        }
        basicInfo.setTotalDistance((float)Math.round(basicInfo.getTotalDistance()*10)/10);
        return basicInfo;
    }
}
